package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.app.domain.AppActivity;
import com.ruoyi.app.domain.AppCreditRules;
import com.ruoyi.app.mapper.AppActivityMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.mapper.AppActivityRegMapper;
import com.ruoyi.app.domain.AppActivityReg;
import com.ruoyi.app.service.IAppActivityRegService;

/**
 * 活动报名Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class AppActivityRegServiceImpl extends ServiceImpl<AppActivityRegMapper, AppActivityReg> implements IAppActivityRegService {
    @Autowired
    private AppActivityRegMapper appActivityRegMapper;
    @Autowired
    private AppActivityMapper appActivityMapper;

    /**
     * 查询活动报名
     *
     * @param id 活动报名主键
     * @return 活动报名
     */
    @Override
    public AppActivityReg selectAppActivityRegById(Long id) {
        AppActivityReg entity = appActivityRegMapper.selectById(id);
        return entity;
    }

    /**
     * 查询活动报名列表
     *
     * @param appActivityReg 活动报名
     * @return 活动报名
     */
    @Override
    public List<AppActivityReg> selectAppActivityRegList(AppActivityReg appActivityReg) {
        MPJLambdaWrapper<AppActivityReg> lqw = buildQueryWrapper(appActivityReg);
        return appActivityRegMapper.selectList(lqw);
    }

    private MPJLambdaWrapper<AppActivityReg> buildQueryWrapper(AppActivityReg appActivityReg) {
        Map<String, Object> params = appActivityReg.getParams();
        MPJLambdaWrapper<AppActivityReg> lqw = new MPJLambdaWrapper<AppActivityReg>()
                .selectAll(AppActivityReg.class)
                .selectAs(AppActivity::getCreateTime,AppActivityReg::getCreateTime2)
                .selectAs(AppActivity::getTitle,AppActivityReg::getActivityName)
                .selectAs(SysUser::getUserName,AppActivityReg::getStudentName)
                .select(AppCreditRules::getRuleName,AppCreditRules::getPoints)
                .leftJoin(AppActivity.class,AppActivity::getActivityId,AppActivityReg::getActivityId)
                .leftJoin(SysUser.class,SysUser::getUserId,AppActivityReg::getUserId)
                .leftJoin(AppCreditRules.class,AppCreditRules::getRuleId,AppActivity::getRuleId);
        lqw.eq(appActivityReg.getActivityId() != null, AppActivityReg::getActivityId, appActivityReg.getActivityId());
        lqw.eq(appActivityReg.getUserId() != null, AppActivityReg::getUserId, appActivityReg.getUserId());
        lqw.eq(StringUtils.isNotBlank(appActivityReg.getStatus()), AppActivityReg::getStatus, appActivityReg.getStatus());
        lqw.orderByDesc(AppActivityReg::getCreateTime);
        return lqw;
    }

    /**
     * 新增活动报名
     *
     * @param appActivityReg 活动报名
     * @return 结果
     */
    @Override
    public int insertAppActivityReg(AppActivityReg appActivityReg) {
        return appActivityRegMapper.insert(appActivityReg);
    }

    /**
     * 修改活动报名
     *
     * @param appActivityReg 活动报名
     * @return 结果
     */
    @Override
    public int updateAppActivityReg(AppActivityReg appActivityReg) {
        return appActivityRegMapper.updateById(appActivityReg);
    }

    /**
     * 批量删除活动报名
     *
     * @param ids 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityRegByIds(Long[] ids) {
        return appActivityRegMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除活动报名信息
     *
     * @param id 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteAppActivityRegById(Long id) {
        return appActivityRegMapper.deleteById(id);
    }

    @Override
    public int submitAppActivityReg(AppActivityReg appActivityReg) {
        AppActivity appActivity = appActivityMapper.selectById(appActivityReg.getActivityId());
        Long l = appActivityRegMapper.selectCount(
                new LambdaQueryWrapper<AppActivityReg>()
                        .eq(AppActivityReg::getActivityId, appActivityReg.getActivityId())
                        .eq(AppActivityReg::getStatus, "1"));
        if (l >= appActivity.getMaxCount()) {
            throw new ServiceException("已经满了,不能报名了!");
        }
        Long l1 = appActivityRegMapper.selectCount(
                new LambdaQueryWrapper<AppActivityReg>()
                        .eq(AppActivityReg::getActivityId, appActivityReg.getActivityId())
                        .eq(AppActivityReg::getUserId, SecurityUtils.getLoginUser().getUserId())
                        .in(AppActivityReg::getStatus, Arrays.asList("0","1")));
        if (l1 >0) {
            throw new ServiceException("不能重复报名,等待审核!");
        }
        AppActivityReg reg = new AppActivityReg();
        reg.setActivityId(appActivityReg.getActivityId());
        reg.setUserId(SecurityUtils.getUserId());
        reg.setStatus("0");
        reg.setCreateBy(SecurityUtils.getUsername());
        reg.setCreateTime(DateUtils.getNowDate());
        appActivityRegMapper.insert(reg);
        return 1;
    }

    @Override
    public int auditAppActivityReg(AppActivityReg appActivityReg) {
        appActivityRegMapper.selectById(appActivityReg.getId());
        appActivityReg.setUpdateBy(SecurityUtils.getUsername());
        appActivityReg.setUpdateTime(DateUtils.getNowDate());
        appActivityReg.setStatus(appActivityReg.getStatus());
        appActivityRegMapper.updateById(appActivityReg);
        return 1;
    }
}
